package com.hefu.virtuallife.ui.foods.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dz.utlis.ClassTools;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.nohttp.dialog.WaitDialog;
import com.hefu.virtuallife.nohttp.util.Toast;
import com.hefu.virtuallife.ui.foods.PreSureActivity;
import com.hefu.virtuallife.ui.foods.bean.FoodInfoBean;
import com.hefu.virtuallife.ui.foods.bean.PlanFoodBean;
import com.hefu.virtuallife.utils.Constants;
import com.hefu.virtuallife.utils.UiCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * creat_user: zhengzaihong
 * email:1096877329@qq.com
 * creat_date: 2018/8/11
 * creat_time: 14:38
 * describe: 预订计划适配器
 **/
public class PrePlanAdapter extends BaseQuickAdapter<PlanFoodBean, BaseViewHolder> {

    private int type = 0;
    private int choosetype = 0;

    private WaitDialog waitDialog;
    private Context context;

    public PrePlanAdapter(Context context) {
        super(R.layout.itme_fragment_plan_foods);
        this.context = context;
        waitDialog = new WaitDialog(context);
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setChooseType(int choosetype) {
        this.choosetype = choosetype;
    }


    @Override
    protected void convert(BaseViewHolder helper, PlanFoodBean item) {

        helper.setText(R.id.tv_foods_type, Constants.getWeek(helper.getLayoutPosition()) + Constants.getTypeFood(choosetype));
        TextView updatefood = helper.getView(R.id.tv_update_food);

        RecyclerView recyclerView = helper.getView(R.id.recycler_foods);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

        if (helper.getLayoutPosition() % 2 == 0) {
            helper.getView(R.id.tv_delect).setBackgroundResource(R.drawable.bg_corners_cannotdel_button);
            ((TextView) helper.getView(R.id.tv_delect)).setTextColor(UiCompat.getColor(mContext.getResources(), R.color.line));
        } else {
            helper.getView(R.id.tv_delect).setBackgroundResource(R.drawable.bg_corners_delect_button);
            ((TextView) helper.getView(R.id.tv_delect)).setTextColor(UiCompat.getColor(mContext.getResources(), R.color.black));
        }
        if (type != 0) {
            updatefood.setText("添加");
            updatefood.setBackgroundResource(R.drawable.bg_corners_preaddfood_button);
        } else {
            updatefood.setText("换一批");
            updatefood.setBackgroundResource(R.drawable.bg_corners_update_foods_button);
        }

        PreItmePlanFoodAdapter preItmePlanFoodAdapter = new PreItmePlanFoodAdapter();
        recyclerView.setAdapter(preItmePlanFoodAdapter);

        List<FoodInfoBean> listbean = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            FoodInfoBean foodInfoBean = new FoodInfoBean();
            listbean.add(foodInfoBean);
        }
        preItmePlanFoodAdapter.replaceData(listbean);

        helper.getView(R.id.tv_prechoose).setOnClickListener(view -> {

            ClassTools.toAct(mContext, PreSureActivity.class);
        });
        helper.getView(R.id.tv_update_food).setOnClickListener(view -> {
            /*if (null != changeFoodListener) {
                changeFoodListener.changefoods(helper.getLayoutPosition());
            }*/
            waitDialog.show();
            if (type != 0) {

            } else {
                new Thread(new UpdataFood(context, preItmePlanFoodAdapter)).start();
            }
        });

    }

    private ChangeFoodListener changeFoodListener;

    public void setChangeFoodListener(ChangeFoodListener listener) {
        this.changeFoodListener = listener;
    }

    public interface ChangeFoodListener {
        void changefoods(int week);
    }

    //更新食物 换一批
    class UpdataFood implements Runnable {
        private Context context;
        private PreItmePlanFoodAdapter preItmePlanFoodAdapter;
        List<FoodInfoBean> list = new ArrayList<>();

        public UpdataFood(Context context, PreItmePlanFoodAdapter preItmePlanFoodAdapter) {
            this.context = context;
            this.preItmePlanFoodAdapter = preItmePlanFoodAdapter;

        }

        @Override
        public void run() {
            Random random = new Random();
            int num = random.nextInt(10);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < num + 3; i++) {
                FoodInfoBean foodInfoBean = new FoodInfoBean();
                list.add(foodInfoBean);
            }
            handler.sendEmptyMessage(0xffff);
        }


        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                waitDialog.dismiss();
                preItmePlanFoodAdapter.replaceData(list);
                Toast.show(mContext, "更新成功");
            }
        };
    }


}
